lib.use(['element', 'table', 'form','laydate','tablePlug','notice'], function () {
    var notice=layui.notice;
    var table = layui.table;
    var layer = layui.layer;
    var laydate = layui.laydate
    var form = layui.form;
    var outHeadTable;
    var searchFrm = layui.Form('searchFrm');
    var tablePlug = layui.tablePlug //表格插件

    laydate.render({elem: '#outDate' ,range: '至'});
    laydate.render({elem: '#checkDate' ,range: '至'});



    table.set(common.layui.table.config(tablePlug));

    searchFrm.selectRender();

    // 渲染表格
    outHeadTable = table.render({
        elem: '#outHeadTable'
        , url: common.util.createUrl('/inv/outHead/list')
        // 对分页请求的参数：page、limit重新设定名称
        , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
        , height: 'full-80'
        , cols: [[
             {type:'checkbox'}
            ,{type: 'numbers', title: '编号' }
            ,{ field: 'id',  hide: true }
            ,{ field: 'entId',  hide: true}
            ,{ field: 'id',  hide: true}
            ,{ title: '出库编码', field: 'outNo' }
            ,{ title: '出库日期', field: 'outDate' }
           /* ,{ title: '门店id', field: 'storeId',  hide: true}
            ,{ title: '门店名称', field: 'storeName' }*/
            ,{ field: 'warehouseId',  hide: true}
            ,{ title: '仓库编码', field: 'warehouseCode' }
            ,{ title: '仓库名称', field: 'warehouseName' }
            ,{ title: '订单状态', field: 'outState' ,templet: function (row) { return common.dist.getValue("inv.OutHead_OutStateEnum",row.outState);}}
            ,{ field: 'checkUserId',  hide: true}
            ,{ field: 'checkUserName',  hide: true}
            ,{ title: '审核时间', field: 'checkDate' }
            ,{ title: '出库类型', field: 'outType' ,templet: function (row) { return common.dist.getValue("inv.OutHead_OutTypeEnum",row.outType);}}
            ,{ field: 'remark',  hide: true}
           /* ,{ field: 'createUserId',  hide: true}
            ,{ title: '创建人', field: 'createUserName',  hide: true}*/
            ,{ field: 'createTime',  hide: true}
           /* ,{ title: '修改人id', field: 'updateUserId',  hide: true}
            ,{ title: '修改人', field: 'updateUserName' }
            ,{ title: '修改时间', field: 'updateTime' }*/
        ]]
       /* ,done: function () {
            layer.closeAll('loading');
        }*/
    });
    //监听行单击事件（单击事件为：rowDouble）
    table.on('rowDouble(outHeadTableFilter)', function(obj){
        update(obj)
    });
    $(document).on('click','#del',function(){
        del(table.checkStatus('outHeadTable'))
    });
    $(document).on('click','#check',function(){
        check(table.checkStatus('outHeadTable'))
    });
    $(document).on('click','#add',function(){
        var config = {
            title:"新增出库单",
            content: 'inv/outHead/outHeadForm.html'
        }
        common.util.openWindow(busCommon.windowConfig(config))
    });
    $(document).on('click','#export',function(){
        var params=common.from.serializeObject('#searchFrm');
        params['pages']=outHeadTable.config.page.pages;
        params['limit']=outHeadTable.config.page.limit;
        common.util.exportWindows('/inv/outHead/export',params)
    });
    form.on('submit(searchFilter)', function(data){
        var param = data.field;
        searchTable(param)
        return false;
    });


    /**
     * 查询表格
     * @param params
     */
    function searchTable(params) {
            params=common.from.serializeObject('#searchFrm');

        tablePlug.getIns(outHeadTable.config.id).reload({where:params});

    }
    /**
     * 修改
     * @param obj
     */
    function update(obj) {
        if(!common.auth.hasPermission("inv:outHead:update")){return;}
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item['id'];
            });
        }else{
            postData=[obj.data.id]
        }
        if(postData.length!=1)return;
        var config = {
            title:"修改出库单",
            content: 'inv/outHead/outHeadForm.html?id=' + postData[0]
        }
        common.util.openWindow(busCommon.windowConfig(config))
    }
    /**
     * 删除
     * @param obj
     */
    function del(obj) {
        if(!common.auth.hasPermission("inv:outHead:delete")){return;}
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item['id'];
            });
        }else{
            postData=[obj.data.id]
        }

        if(postData.length!=1){common.util.error('请选择一条记录进行操作');return;}
        common.util.confirm('确认删除!', function(index){
            console.log('Config Del param is', postData);
            common.ajax.get('/inv/outHead/delete/'+postData[0],null, function (resp) {
                searchTable();
                layer.close(index);
            })

        });
    }

    /**
     * 审核
     * @param obj
     */
    function check(obj) {
        if(!common.auth.hasPermission("inv:outHead:check")){return;}
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item['id'];
            });
        }else{
            postData=[obj.data.id]
        }

        if(postData.length!=1){common.util.error('请选择一条记录进行操作');return;}
        common.util.confirm('确认审核!', function(index){
            console.log('Config Check param is', postData);
            common.ajax.get('/inv/outHead/check/'+postData[0], null, function (resp) {
                searchTable();
                layer.close(index);
                common.util.success("审核成功")
            })

        });
    }
    window.formCallBack=function (params) {
        searchTable(params);
    };
});
